package ln.javatest.day01.demo01;

import java.sql.*;

public class JDBCDemo03 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //定义sql对象
            String sql = "select * from student";
            //获取连接对象
            conn = DriverManager.getConnection("jdbc:mysql:///db1", "root", "123456");
            //获取执行sql对象
            stat = conn.createStatement();
            //执行sql(DQL用executeQuery()来执行）
            rs = stat.executeQuery(sql);
            //处理结果
            //循环判断游标是否是最后一行的末尾
            while(rs.next()){
                //获取数据
                int id = rs.getInt(1);
                String namee = rs.getString("namee");
                int sorce = rs.getInt(1);
                String sex = rs.getString("sex");
                int age = rs.getInt(1);
                System.out.println(id+"--"+namee+"--"+sorce+"--"+sex+"--"+"age");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            if(stat!= null){
                try {
                    stat.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn!= null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(rs!= null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
